﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Configuration;
using System.Windows;
using System.Threading;

namespace Incendios0_1.tools
{
    class SerialPortManager
    {
        public static SerialPort cameraPort;
        private static bool cameraPortOpen;
        private static bool smsPortOpen;
        public static bool CameraPortOpen
        {
            get
            {
                return cameraPortOpen;
            }
        }
        public static bool SMSPortOpen
        {
            get
            {
                return smsPortOpen;
            }
        }
        public static SerialPort smsPort;

        public static void initCameraPort()
        {
            if (!ConfigurationManager.AppSettings["CameraSerialPort"].Equals("None"))
            {
                cameraPort = new SerialPort();
                cameraPort.BaudRate = 9600;
                cameraPort.PortName = ConfigurationManager.AppSettings["CameraSerialPort"];
                cameraPort.Parity = Parity.None;
                cameraPort.DataBits = 8;
                cameraPort.StopBits = StopBits.One;
                cameraPort.Handshake = Handshake.None;
                cameraPort.ReadTimeout = 1000;
                try
                {
                    cameraPort.Open();
                    cameraPortOpen = true;
                }
                catch (Exception)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("Puerto " + ConfigurationManager.AppSettings["CameraSerialPort"] + " ocupado o inaccesible \n La aplicación es incapaz de controlar las camaras", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    cameraPortOpen = false;
                }
            }
        }

        public static void initSMSPort()
        {
            if (!ConfigurationManager.AppSettings["SMSPort"].Equals("None"))
            {
                smsPort = new SerialPort();
                smsPort.PortName = ConfigurationManager.AppSettings["SMSPort"]; ;
                smsPort.BaudRate = 9600;                   //9600
                smsPort.DataBits = 8;                   //8
                smsPort.StopBits = StopBits.One;                  //1
                smsPort.Parity = Parity.None;                     //None
                smsPort.ReadTimeout = 300;             //300
                smsPort.WriteTimeout = 300;           //300
                smsPort.Encoding = Encoding.GetEncoding("iso-8859-1");
                smsPort.DtrEnable = true;
                smsPort.RtsEnable = true;
                try
                {
                    smsPort.Open();
                    smsPortOpen = true;
                }
                catch (Exception)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("Puerto " + ConfigurationManager.AppSettings["SMSPort"] + " ocupado o inaccesible \n La aplicación es incapaz de enviar mensajes SMS", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    smsPortOpen = false;
                }
            }
        }

        internal static void ClosePorts()
        {
            if (cameraPortOpen)
                cameraPort.Close();
            if (smsPortOpen)
                smsPort.Close();
        }

        internal static void InitPorts()
        {
            initCameraPort();
            initSMSPort();
        }

        internal static void InitPorts(LoadState state, int p)
        {
            state.CurrentOperation = "Inicializando Puertos Seriales";
            state.OperationMessage = String.Format("Inicialicando puerto de control de camaras...");
            initCameraPort();
            state.Proggresion += p/2;
            state.OperationMessage = String.Format("Inicialicando puerto de mensajes SMS...");
            initSMSPort();
            state.Proggresion += p / 2;
        }
    }
}
